package com.xq.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * 时间处理工具
 *
 * @author xu
 * 2021/8/4 15:14
 */
public class DateUtils {
    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
    public static String YYYYMMDDHHMM = "yyyyMMddHHmm";
    public static String YYYYMMDDHH = "yyyyMMddHH";

    /**
     * localDate 转 date
     * @param localDateTime
     * @return
     */
    public static Date localToDate(LocalDateTime localDateTime){
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = localDateTime.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * String 转 date
     */
    public static Date strToDate(String str){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYYMMDDHHMMSS);
        try {
            return simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * String 转 date
     * 并向后推移n个小时
     */
    public static Date strToDatePlusHours(int n,String str){
        DateTimeFormatter df = DateTimeFormatter.ofPattern(YYYYMMDDHHMMSS);
        LocalDateTime dateTime = LocalDateTime.parse(str,df);
        LocalDateTime plusHours = dateTime.plusHours(n);
        return localToDate(plusHours);
    }

    /**
     * 获取向前一个小时的时间 示例 20210804140000
     * @return
     */
    public static String dateTimeMinusOneHour(){
        LocalDateTime now = LocalDateTime.now();
        //向前推1个小时
        LocalDateTime minusOneHour = now.minusHours(1);
        // 转换格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHH);
        return minusOneHour.format(formatter)+"0000";
    }

    /**
     * 获取当前时间的向前n天
     * @param nDay
     * @return
     */
    public static LocalDateTime dateTimeMinusDay(int nDay){
        LocalDateTime now = LocalDateTime.now();
        return now.minusDays(nDay);
    }

    /**
     * 获取当前时间的向前n个小时
     * @param nHour
     * @return
     */
    public static LocalDateTime dateTimeMinusHourThe(int nHour){
        LocalDateTime now = LocalDateTime.now();
        return now.minusHours(nHour);
    }

    /**
     * 向前推5分钟 并将秒化为0
     * @return
     */
    public static String dateTimeMinusFiveMin(){
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime minusFiveMin = now.minusHours(8).minusMinutes(5);
        // 转换格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHHMM);
        return minusFiveMin.format(formatter)+"00";
    }

    /**
     * 获取向前n个小时的时间 示例 20210804140000
     * @return
     */
    public static String dateTimeMinusHour(int nHour){
        LocalDateTime now = LocalDateTime.now();
        //向前推1个小时
        LocalDateTime minusOneHour = now.minusHours(nHour);
        // 转换格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YYYYMMDDHH);
        return minusOneHour.format(formatter)+"0000";
    }



    /**
     * 获取当前时间的String类型
     * 格式为：2021081810
     */
    public static String getNowDate() {
        LocalDateTime now = LocalDateTime.now();
        return getDate(YYYYMMDDHH, now);
    }

    public static String getNowDateTime(){
        LocalDateTime now = LocalDateTime.now();
        return getDate(YYYYMMDDHHMMSS,now);
    }

    /**
     * 获取时间的string类型转换
     *
     * @param pattern       转换格式
     * @param localDateTime 时间
     */
    public static String getDate(String pattern, LocalDateTime localDateTime) {
        // 转换格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(pattern);
        return localDateTime.format(formatter);
    }

    public static void main(String[] args) {
        System.out.println(dateTimeMinusDay(2));
    }
}
